<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="com.google.appengine.api.users.*" %>
<%@ page import="com.honkentuber.*" %>
<%@ page import="com.honkentuber.chat.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.Query" %>

<html><body>
<%
  PersistenceManager pm = PMF.get().getPersistenceManager();
  UserService userService = UserServiceFactory.getUserService();
  String requestUri = request.getRequestURI();
  String loginUrl = userService.createLoginURL(requestUri);
  String logoutUrl = userService.createLogoutURL(requestUri);
  User currentUser = userService.getCurrentUser();
  if (currentUser != null) {
%>
user: <%=currentUser.getEmail()%>
<a href="<%= logoutUrl %>">sign out</a>
<%
  } else {
%>
<a href="<%= loginUrl %>">sign in</a>
<%
  }
%>
<table border=1>
<tr>
<td>Users</td>
<td>key</td>
<td>userId</td>
<td>nickName</td>
<td>email</td>
</tr>
<%
Query query = pm.newQuery(MyUser.class);
for (MyUser user : (List<MyUser>) query.execute()) {
  Key key = user.getKey();
  String keyString = KeyFactory.keyToString(key);
%>
<tr>
<td><a href="deleteuser?html=1&key=<%= keyString %>">Delete</a></td>
<td><%= key.toString() %></td>
<td><%= user.getUserId() %></td>
<td><%= user.getNickName() %></td>
<td><%= user.getEmail() %></td>
</tr>
<%
}
%>
</table><hr>
<form action="/newroom?html=1" method="post">
  <label for="name">
    <input id="name" name="name" size="64">
  </label>
  <input type="submit" value="New room" />
</form>
<table border=1>
<tr>
<td>Rooms</td>
<td>key</td>
<td>creator</td>
<td>name</td>
<td>present</td>
</tr>
<%
query = pm.newQuery(Room.class);
List<Room> rooms = (List<Room>) query.execute();
for (Room room : rooms) {
  Key roomKey = room.getKey();
  String roomKeyString = KeyFactory.keyToString(roomKey);
  String present = "";
  for (Presence presence : room.getPresences()) {
    if (present.length() != 0) {
      present += ",";
    }
    present += presence.toString();
  }
  Key creatorKey = room.getCreatorKey();
%>
<tr>
<td><a href="deleteroom?html=1&key=<%= roomKeyString %>">Delete</a></td>
<td><%= roomKey %></td>
<td><%= (creatorKey == null) ? "default" : creatorKey.toString() %></td>
<td><a href="/chat?html=1&form=1&roomKey=<%= roomKeyString %>">
  <%= room.getName() %>
</a></td>
<td><%= present %></td>
</tr>
<%
}
%>
</table>
<hr>
<table border=1>
<tr>
<td>Messages</td>
<td>key</td>
<td>seq</td>
<td>author</td>
<td>content</td>
<td>ms</td>
<td>room</td>
</tr>
<%
query = pm.newQuery(Message.class);
List<Message> msgs = (List<Message>) query.execute();
for (Message msg : msgs) {
  Key msgKey = msg.getKey();
  String msgKeyString = KeyFactory.keyToString(msgKey);
  Key roomKey = msg.getRoomKey();
  String roomKeyString = KeyFactory.keyToString(roomKey);
  Key authorKey = msg.getAuthorKey();
%>
<tr>
<td><a href="deletemsg?html=1&id=<%= msgKeyString %>">Delete</a></td>
<td><%= msgKey.toString() %></td>
<td><%= msg.getSequenceNumber() %></td>
<td><%= (authorKey == null) ? "anonymous" : authorKey.toString() %></td>
<td><%= msg.getContent() %></td>
<td><%= msg.getTimeMillis() %></td>
<td><a href="/chat?html=1&form=1&roomKey=<%= roomKeyString %>">
  <%= roomKey.toString() %>
</a></td>
</tr>
<%
}
%>
</table>
</body></html>